<?php 
/************************************ 
 FILE           : alexa-api.class.php 
 Description    : To get all the contents form alexa for perticuler Website Like (RelatedLinks, Rank,OwnedDomains etc..) 
 VERSION        : 1.0                            
 AUTHOR         : Kunjesh J Sukhadia 
 E-Mail         : kunjesh.sukhadia@gmail.com 
 Last update    : 17 October 2008 
**************************************/ 

class AlexaAPI 
{ 
    var $mBaseUrl               = ""; 
    var $mActionName            = ""; 
    var $mAccessKey             = ""; 
    var $mSecureKey             = ""; 
    var $mTimeStamp             = ""; 
    var $mSignature             = ""; 
    var $mData                  = ""; 
    var $mAlaxaUrl              = ""; 
    var $mUrl                   = ""; 
    var $mReletedLinksRequests  = ""; 
    var $mResourseGroup         = ""; 
    var $mError                 = ""; 
    function AlexaAPI()                 // Default Constructor of AlexaAPI class 
    { 
        $this->mBaseUrl       = "http://awis.amazonaws.com?"; 
        $this->mActionName    = "UrlInfo"; 
        $this->mAccessKey     = "";    // After registring on alexa web information service site they will give you                                   API Key and secure key put both key in this variable. 
        $this->mSecureKey     = ""; 
        $this->mResourseGroup = array(); 
    } 
    function Hmacsha1($key,$data) 
    {     
        $b = 64;  
        if (strlen($key) > $b)  
        { 
            $key = pack("A*",sha1($key,true)); 
        } 
        $key  = str_pad($key, $b, chr(0x00)); 
        $ipad = str_pad('', $b, chr(0x36)); 
        $opad = str_pad('', $b, chr(0x5c)); 
        $k_ipad = $key ^ $ipad ; 
        $k_opad = $key ^ $opad; 

        return sha1($k_opad  . pack("A*",sha1($k_ipad . $data,true)),true); 
    } 

    function GetSign($SecureKey,$data)          // Return 64 bit encoded Signature key 
    { 
        //generate base64 encoded HMAC from $secret and $token 
        $my_key=$SecureKey; 
        $my_data=$data; 

        return base64_encode($this->Hmacsha1($my_key,$my_data));     
    } 

    //this function will retrive alexa statistics from live. 
    function GetAlexaData($url,$responceArray = '') 
    { 
        if("" != $url) 
        { 
            if(empty($responceArray)) 
            { 
                $this->mResourseGroup   = array('RelatedLinks','Categories','Rank','RankByCountry','RankByCity','UsageStats','ContactInfo','AdultContent','Speed','Language','Keywords','OwnedDomains','LinksInCount','SiteData');
                $relatedlink_string     = implode(",",$this->mResourseGroup); 
                $this->Url              = urlencode($url); 
                $this->mTimeStamp       = gmdate('Y-m-d\TH:i:s') . '.000Z'; 
                $this->mData            = $this->mActionName.$this->mTimeStamp; 
                $this->mSignature       = urlencode($this->GetSign($this->mSecureKey,$this->mData)); 
                $total_response_group   = 0;         
                $this->mAlaxaUrl        = $this->mBaseUrl."Operation=".$this->mActionName.'&AWSAccessKeyId=' .$this->mAccessKey. '&Signature=' . $this->mSignature . '&Timestamp=' . $this->mTimeStamp .'&ResponseGroup='.$relatedlink_string.'&Url='.$this->Url; 
                $get_curl_data = $this->ProcessCurl($this->mAlaxaUrl); 
                 
                return $get_curl_data; 
            } 
        } 
        else 
        { 
            $this->mError = "Url not enterd"; 
            return false; 
        } 
    } 

    // This function will fired query through curl. 
    function ProcessCurl($alexaUrl) 
    { 
        $ch = curl_init($this->mAlaxaUrl); 
         
        curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $processed_url = curl_exec($ch); 
        curl_close($ch); 
        return $processed_url; 
    } 

    function GetError() 
       {    
        return $this->mError; 
    } 
} 


//// example
//$response_array            = array();
//$url                       = $_GET['q'];
//$response_array            = array('RelatedLinks','Categories','Rank','RankByCountry','RankByCity','UsageStats','ContactInfo','AdultContent','Speed','Language','Keywords','OwnedDomains','LinksInCount','SiteData');
//$alexa_api                 = new AlexaAPI();
//$related_link_result_array = $alexa_api->GetAlexaData($url);
//print_r( $related_link_result_array);
